package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.pojo.CollectRelation;
import com.explore.demo.pojo.RowView;
import com.explore.demo.mapper.RowViewMapper;
import com.explore.demo.service.RowViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-04-21
 */
@Service
public class RowViewServiceImpl extends ServiceImpl<RowViewMapper, RowView> implements RowViewService
{

    @Resource
    private RowViewMapper rowViewMapper;

    @Override
    public ServerResponse addRowView(@NotNull RowView rowView)
    {
        return rowViewMapper.insert(rowView) > 0 ?
               ServerResponse.createBySuccessMessage("插入成功",200,null):
               ServerResponse.createBySuccessMessage("插入失败",2000,null);
    }

    @Override
    public ServerResponse deleteByRecruitId(@NotNull Integer recruitId)
    {
        QueryWrapper<RowView> wrapper = new QueryWrapper<>();
        wrapper.eq("recruit_id",recruitId);
        RowView collectRelation = rowViewMapper.selectOne(wrapper);
        UpdateWrapper<RowView> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("deleted",1);
        return rowViewMapper.update(collectRelation,updateWrapper) != 0?
               ServerResponse.createBySuccessMessage("删除成功",
                                                     200,
                                                     null):
               ServerResponse.createBySuccessMessage("删除失败",
                                                     2000,
                                                     null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<RowView> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     rowViewMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryAllByRecruitId(@NotNull Integer recruitId)
    {
        QueryWrapper<RowView> wrapper = new QueryWrapper<>();
        wrapper
                .eq("recruit_id",recruitId);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     rowViewMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse updateRowView(@NotNull RowView rowView)
    {
        return rowViewMapper.updateById(rowView) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
